package com.example.takeaway.utils;

import java.util.Random;

/**
 * 随机生成验证码工具类
 */
public class ValidateCodeUtils {


    /**
     * 随机生成验证码
     * @param length
     * @return
     */

    public static Integer generateValidateCode(int length){

        Integer code = null;

        if (length == 4){
            code = new Random().nextInt(9999); //生成随机数 最大为999
            if (code < 1000){
                code = code + 1000;
            }
        }else if(length == 6){

            code = new Random().nextInt(999999); //生成随机数 最大为99999
            if(code < 100000){
                code = code + 100000;
            }

        }else {
            throw new RuntimeException("只能生成4位或者6位的验证码");
        }

        return code;




    }

    /**
     * 生成指定长度字符串验证码
     * @param length 长度
     * @return
     */


    public static String gengerateValidateCode4String(int length){


        Random random = new Random();
        String hexString = Integer.toHexString(random.nextInt());

        String substring = hexString.substring(0, length);
        return substring;


    }


}
